package com.dy.lock.screen.component.impl;

import java.util.Date;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import com.dy.lock.screen.component.CustomerFeedbackComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.CustomerFeedbackInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.CustomerFeedbackService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.CustomerFeedbackVo;
import com.google.gson.Gson;
@Component("customerFeedbackComponent")
public class CustomerFeedbackComponentImpl implements CustomerFeedbackComponent {
	private static final Logger log = Logger.getLogger(CustomerFeedbackComponentImpl.class);
	private Gson gson;
	
	@Resource(name="customerFeedbackService")
	private CustomerFeedbackService customerFeedbackService;
	
	public CustomerFeedbackComponentImpl() {
		gson = CommonUtil.gson();
	}
	
	@Override
	public CustomerFeedbackVo customerFeedBack(String body, Head head) {
		CustomerFeedbackVo vo = gson.fromJson(body, CustomerFeedbackVo.class);
		if (vo != null) {
			CustomerFeedbackInfo info = new CustomerFeedbackInfo();
			BeanUtils.copyProperties(vo,info); 
			info.setCustomerId(vo.getUserId());
			info.setFeedbackTime(new Date());
			info.setIsDelete("1");
			int re = this.customerFeedbackService.save(info);
			if (re > 0) {
				vo.setResult(true);
				return vo;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",反馈意见时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
